//
//  UIImageView+LEWebImage.m
//  CreditAddressBook
//
//  Created by Lee on 15/8/24.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "UIImageView+LEWebImage.h"

@implementation UIImageView (LEWebImage)

- (void)le_setImageWithURL:(NSString *)url {
    [self le_setImageWithURL:url placeholderImage:nil];
}

- (void)le_setImageWithURL:(NSString *)url placeholderImage:(UIImage *)placeholder {
    [self le_setImageWithURL:url placeholderImage:placeholder completed:nil];
}

- (void)le_setImageWithURL:(NSString *)url completed:(LEWebImageCompletedWithFinishedBlock)completedBlock {
    [self le_setImageWithURL:url placeholderImage:nil completed:completedBlock];
}

- (void)le_setImageWithURL:(NSString *)url placeholderImage:(UIImage *)placeholder completed:(LEWebImageCompletedWithFinishedBlock)completedBlock {
    self.image = placeholder;
    
    if (url) {
        __weak UIImageView *wself = self;
        [LEWebImage downloadWithURL:url progress:nil completed:^(UIImage *image, NSError *error, BOOL finished) {
            if (!wself) return;
            dispatch_main_sync_safe_le(^{
                if (!wself) return;
                if (image) {
                    wself.image = image;
                    [wself setNeedsLayout];
                }
                if (completedBlock && finished) {
                    completedBlock(image, error, finished);
                }
            });
        }];
    }
}

@end

